import QtQuick 2.14
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import "components"
import "styles"

Rectangle {
    id: mainDashboard
    width: 1024
    height: 600
    color: AppStyle.backgroundColor

    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 20
        spacing: 20

        // 标题栏
        Text {
            text: "系统仪表板"
            font.pixelSize: 24
            font.bold: true
            color: AppStyle.textColor
            Layout.alignment: Qt.AlignHCenter
        }

        // 状态卡片区域
        GridLayout {
            Layout.fillWidth: true
            columns: 3
            columnSpacing: 20
            rowSpacing: 20

            // 设备状态卡片
            Rectangle {
                Layout.fillWidth: true
                Layout.preferredHeight: 120
                color: AppStyle.cardBackgroundColor
                radius: 8
                border.color: AppStyle.borderColor
                border.width: 1

                ColumnLayout {
                    anchors.centerIn: parent
                    spacing: 10

                    StatusIndicator {
                        id: deviceStatus
                        Layout.alignment: Qt.AlignHCenter
                        status: qmlBridge ? qmlBridge.getDeviceStatus() : "disconnected"
                    }

                    Text {
                        text: "设备状态"
                        font.pixelSize: 14
                        color: AppStyle.textColor
                        Layout.alignment: Qt.AlignHCenter
                    }
                }
            }

            // 采样状态卡片
            Rectangle {
                Layout.fillWidth: true
                Layout.preferredHeight: 120
                color: AppStyle.cardBackgroundColor
                radius: 8
                border.color: AppStyle.borderColor
                border.width: 1

                ColumnLayout {
                    anchors.centerIn: parent
                    spacing: 10

                    Text {
                        text: qmlBridge ? (qmlBridge.isSampling() ? "采样中" : "待机") : "待机"
                        font.pixelSize: 16
                        font.bold: true
                        color: qmlBridge && qmlBridge.isSampling() ? AppStyle.successColor : AppStyle.textColor
                        Layout.alignment: Qt.AlignHCenter
                    }

                    Text {
                        text: "采样状态"
                        font.pixelSize: 14
                        color: AppStyle.textColor
                        Layout.alignment: Qt.AlignHCenter
                    }
                }
            }

            // 系统信息卡片
            Rectangle {
                Layout.fillWidth: true
                Layout.preferredHeight: 120
                color: AppStyle.cardBackgroundColor
                radius: 8
                border.color: AppStyle.borderColor
                border.width: 1

                ColumnLayout {
                    anchors.centerIn: parent
                    spacing: 10

                    Text {
                        text: "正常"
                        font.pixelSize: 16
                        font.bold: true
                        color: AppStyle.successColor
                        Layout.alignment: Qt.AlignHCenter
                    }

                    Text {
                        text: "系统状态"
                        font.pixelSize: 14
                        color: AppStyle.textColor
                        Layout.alignment: Qt.AlignHCenter
                    }
                }
            }
        }

        // 快速操作按钮
        RowLayout {
            Layout.fillWidth: true
            Layout.alignment: Qt.AlignHCenter
            spacing: 20

            CustomButton {
                text: "设备控制"
                onClicked: {
                    if (qmlBridge) {
                        qmlBridge.navigateToPage("DeviceControl")
                    }
                }
            }

            CustomButton {
                text: "定时采样"
                onClicked: {
                    if (qmlBridge) {
                        qmlBridge.navigateToPage("TimedSampling")
                    }
                }
            }

            CustomButton {
                text: "定量采样"
                onClicked: {
                    if (qmlBridge) {
                        qmlBridge.navigateToPage("VolumeSampling")
                    }
                }
            }

            CustomButton {
                text: "数据可视化"
                onClicked: {
                    if (qmlBridge) {
                        qmlBridge.navigateToPage("DataVisualization")
                    }
                }
            }
        }

        // 最近操作记录
        Rectangle {
            Layout.fillWidth: true
            Layout.fillHeight: true
            color: AppStyle.cardBackgroundColor
            radius: 8
            border.color: AppStyle.borderColor
            border.width: 1

            ColumnLayout {
                anchors.fill: parent
                anchors.margins: 20
                spacing: 10

                Text {
                    text: "最近操作记录"
                    font.pixelSize: 16
                    font.bold: true
                    color: AppStyle.textColor
                }

                ListView {
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                    model: qmlBridge ? qmlBridge.getRecentOperations() : []
                    
                    delegate: Rectangle {
                        width: parent.width
                        height: 40
                        color: index % 2 === 0 ? "transparent" : AppStyle.alternateRowColor

                        RowLayout {
                            anchors.fill: parent
                            anchors.margins: 10
                            spacing: 10

                            Text {
                                text: modelData.timestamp || "未知时间"
                                font.pixelSize: 12
                                color: AppStyle.secondaryTextColor
                                Layout.preferredWidth: 120
                            }

                            Text {
                                text: modelData.operation || "未知操作"
                                font.pixelSize: 12
                                color: AppStyle.textColor
                                Layout.fillWidth: true
                            }

                            Text {
                                text: modelData.user || "未知用户"
                                font.pixelSize: 12
                                color: AppStyle.secondaryTextColor
                                Layout.preferredWidth: 80
                            }
                        }
                    }
                }
            }
        }
    }

    // 定时器用于更新状态
    Timer {
        interval: 5000 // 5秒更新一次
        running: true
        repeat: true
        onTriggered: {
            if (deviceStatus) {
                deviceStatus.status = qmlBridge ? qmlBridge.getDeviceStatus() : "disconnected"
            }
        }
    }
}